#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;

class Solution
{
public:
    static int subarraySum(vector<int>& nums, int k)
    {
        unordered_map<int, int> hash;//之前的前缀和
        hash[0] = 1;

        int sum = 0,ret = 0;
        for(auto x : nums)
        {
            sum += x;//当前的前缀和
            if(hash.count(sum-k))
                ret+=hash[sum-k];
            hash[sum]++;
        }
        return ret;
    }
};
